package my.dataCollection.busi.processor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import my.dataCollection.base.IProcessor;
import my.dataCollection.base.vo.HandleVO;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;


public class HTMLProcessor implements IProcessor {

	@Override
    public boolean Handle(HandleVO vo) {

		try{
			Document doc = Jsoup.parse(vo.getCrawlerStr());
			
			Map<String, String> map = vo.getHtmlSelectorsMap();
			Map<String, Object> rsMap = new HashMap<String, Object>();
			
			if(map.isEmpty()){
				return false;
			}
			
			Set<String> keys = map.keySet();
			
			for(String key : keys){
				Elements elements = doc.select(map.get(key));

				for(int i=0; i<elements.size(); i++){
					Element element = elements.get(i);
					List<Node> childNodes = element.childNodes();
					
					rsMap.put(key, childNodes);
				}
				
			}
			
			vo.setResultSet(rsMap);
		}catch (Exception e) {
			return false;
		}
	    return true;
    }

}
